package com.itheima.service.db;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.domain.db.Log;
import com.itheima.domain.db.UserInfo;
import com.itheima.mapper.LogMapper;
import com.itheima.mapper.UserInfoMapper;
import com.itheima.vo.ReturnDateVo;
import com.itheima.vo.UsersVo;
import org.apache.commons.lang.time.DateUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@DubboService
public class UserYearImpl implements UserYear {
    @Autowired
    UserInfoMapper userInfoMapper;
    @Autowired
    LogMapper logMapper;

    //数据统计
    @Override
    public UsersVo users(Date startTime, Date endTime, String type) {
        UsersVo usersVo = new UsersVo();
        switch (type) {
            case "101":  //新增
                usersVo = findAddUsers(startTime, endTime);
                break;
            case "102":  //活跃用户
                usersVo = findActiveUsers(startTime, endTime);
                break;
            case "103":  //次日留存
                usersVo = findRetention(startTime, endTime);
                break;
        }
        return usersVo;
    }


    //次日留存
    private UsersVo findRetention(Date startTime, Date endTime) {
        UsersVo usersVo = new UsersVo();
        //今年
        List<ReturnDateVo> thisYesr = getfindRetention(startTime, endTime);
        usersVo.setThisYear(thisYesr);
        //去年 今年减一
        Date lastStartYesr = DateUtils.addYears(startTime, -1);
        Date lastendYesr = DateUtils.addYears(endTime, -1);
        List<ReturnDateVo> lastYesr = getfindRetention(lastStartYesr, lastendYesr);
        usersVo.setLastYear(lastYesr);
        return usersVo;
    }

    //次日留存折线
    private List<ReturnDateVo> getfindRetention(Date startTime, Date endTime) {
        List<ReturnDateVo> thisYear = new ArrayList<>();
        long sd = startTime.getTime(); //返回long 毫秒值
        long ed = endTime.getTime();
        //查询用户信息表
        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        List<Log> userInfos = logMapper.selectList(queryWrapper);
        //最后一次登录表
        List<Log> userInfoList = new ArrayList<>();
        //创建表
        List<Log> userInfoList1 = new ArrayList<>();


        for (Log userInfo : userInfos) {
            if (userInfo.getUpdated().getTime() > sd && userInfo.getUpdated().getTime() < ed) {
                userInfoList.add(userInfo);
            }
            if (userInfo.getCreated().getTime() > sd && userInfo.getCreated().getTime() < ed) {
                userInfoList1.add(userInfo);
            }


        }
        long day = (ed - sd) / 24 / 3600 / 1000; //间隔多少天
        long increase = 24 * 3600 * 1000; //新增 单位一天 多一天算新增
        if (day > 30) { //月增长
            day = day / 30;
            increase = 30L * 24 * 3600 * 1000;
        }
        //假如2号开始七天
        for (int i = 1; i <= day; i++) {
            ReturnDateVo returnDateVo = new ReturnDateVo();
            long count = 0;
            for (Log userInfo : userInfoList) {
                for (Log userInfo1 : userInfoList1) {
                    Date crTim = DateUtils.addDays(new Date(userInfo.getUpdated().getTime()), -1);//推测创建时间
                    Date crTim1 = new Date(userInfo1.getCreated().getTime());//实际创建时间
                    if (userInfo.getUpdated().getTime()>sd && userInfo.getUpdated().getTime()<sd+increase&&DateUtil.formatDate(crTim).equals(DateUtil.formatDate(crTim1)) && userInfo.getUserId()==(userInfo1.getUserId())) {
                        count++;
                    }

                }

            }
            returnDateVo.setTitle(i + "");//日期
            returnDateVo.setAmount(count);//数量
            thisYear.add(returnDateVo);//封装对象放到列表里
            sd += increase;//从下一天开始

        }
        return thisYear;

    }



    //活跃用户
    private UsersVo findActiveUsers(Date startTime, Date endTime) {
        UsersVo usersVo = new UsersVo();
        //今年
       List<ReturnDateVo> thisYear= getActiveUser(startTime,endTime);
       usersVo.setThisYear(thisYear);
       //去年
        Date lastYearStart = DateUtils.addYears(startTime, -1);
        Date lastYearend = DateUtils.addYears(endTime, -1);
        List<ReturnDateVo> lastYear= getActiveUser(lastYearStart,lastYearend);
        usersVo.setLastYear(lastYear);
        return usersVo;
    }
    //活跃用户折线
    private List<ReturnDateVo> getActiveUser(Date startTime, Date endTime) {
      List<ReturnDateVo> thisYear = new ArrayList<>();
        long sd = startTime.getTime(); //返回long 毫秒值
        long ed = endTime.getTime();
        //查询用户信息表
        QueryWrapper<Log> queryWrapper = new QueryWrapper<>();
        List<Log> userInfos = logMapper.selectList(queryWrapper);
        List<Log> userInfoList = new ArrayList<>();

        for (Log userInfo : userInfos) {
            if (userInfo.getUpdated().getTime()>sd && userInfo.getUpdated().getTime()<ed) {
                userInfoList.add(userInfo);
            }
        }
        long day = (ed - sd) / 24 / 3600 / 1000; //间隔多少天
        long increase=24*3600*1000; //新增 单位一天 多一天算新增
        if (day>30) { //月增长
            day=day/30;
            increase=30L*24*3600*1000;
        }
        //假如2号开始七天
        for (int i = 1; i <=day ; i++) {
            ReturnDateVo returnDateVo = new ReturnDateVo();
            long count=0;
            for (Log userInfo : userInfoList) {
                //大于当天(或月)小于明天(或月)算活跃
                if (userInfo.getCreated().getTime()>=sd && userInfo.getCreated().getTime()<=sd+increase) {
                    count++;
                }

            }
            returnDateVo.setTitle(i+"");//日期
            returnDateVo.setAmount(count);//数量
            thisYear.add(returnDateVo);//封装对象放到列表里
            sd+=increase;//从下一天开始

        }
        return thisYear;





    }







    //新增用户
    private UsersVo findAddUsers(Date startTime, Date endTime) {
        UsersVo usersVo = new UsersVo();
        //今年
        List<ReturnDateVo> thisYesr=getAddUserBrokenLine(startTime,endTime);
        usersVo.setThisYear(thisYesr);
        //去年 今年减一
       Date lastStartYesr= DateUtils.addYears(startTime,-1);
       Date lastendYesr= DateUtils.addYears(endTime,-1);
       List<ReturnDateVo> lastYesr=getAddUserBrokenLine(lastStartYesr,lastendYesr);
       usersVo.setLastYear(lastYesr);
       return usersVo;
    }

    //新增用户折线
    private List<ReturnDateVo> getAddUserBrokenLine(Date startTime, Date endTime) {
        List<ReturnDateVo> thisYear = new ArrayList<>();
        long sd = startTime.getTime(); //返回long
        long ed = endTime.getTime();
        //查询用户信息表
        QueryWrapper<UserInfo> queryWrapper = new QueryWrapper<>();
        List<UserInfo> userInfos = userInfoMapper.selectList(queryWrapper);
        List<UserInfo> userInfoList = new ArrayList<>();
        //迭代符合条件的日期放入集合
        for (UserInfo userInfo : userInfos) {
            if (userInfo.getCreated().getTime()>sd && userInfo.getCreated().getTime()<ed) {
                userInfoList.add(userInfo);
            }

        }
        long day = (ed - sd) / 24 / 3600 / 1000; //间隔多少天
        long increase=24*3600*1000; //新增 单位一天 多一天算新增
        if (day>30) { //月增长
            day=day/30;
            increase=30L*24*3600*1000;
        }
        //假如
        for (int i = 1; i <=day ; i++) {
            ReturnDateVo returnDateVo = new ReturnDateVo();
            long count=0;
            for (UserInfo userInfo : userInfoList) {
                //大于当天小于明天算新增
                if (userInfo.getCreated().getTime()>sd && userInfo.getCreated().getTime()<sd+increase) {
                    count++;
                }

            }
            returnDateVo.setTitle(i+"");//日期
            returnDateVo.setAmount(count);//数量
            thisYear.add(returnDateVo);//封装对象放到列表里
            sd+=increase;//从下一天开始

        }
        return thisYear;


    }



}
